package com.leetcode;

import java.util.Arrays;

/**
 * 462. 最少移动次数使数组元素相等 II
 * 系统排序
 *
 * 2022-05-19 每日一题
 *
 * @author fy
 * @date 2022/5/19 11:32
 */
public class Solution462 {

    public int minMoves2(int[] nums) {
        if (nums == null || nums.length <= 1) {
            return 0;
        }
        Arrays.sort(nums);
        int mid = (nums.length - 1) / 2;
        int midNum = nums[mid];
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            res += Math.abs(nums[i] - midNum);
        }
        return res;
    }

}
